﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using InfoTech.Core.Service;
using InfoTech.LanTranh.MucVu.Bussiness;

namespace InfoTech.LanTranh.MucVu.Service
{
    public class QUOCGIAService : BaseService
    {

        public static bool Check_Insert_QUOCGIA(QUOCGIA quocgia)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<QUOCGIA>().FirstOrDefault(c => c.TENQUOCGIA.ToUpper() == quocgia.TENQUOCGIA.ToUpper() ||
                c.VIETTAT.ToUpper() == quocgia.VIETTAT.ToUpper() ||
                c.MABUUCHINH.ToUpper() == quocgia.MABUUCHINH.ToUpper()) == null;
            return result;
        }

        public static bool Check_Update_QUOCGIA(QUOCGIA quocgia)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<QUOCGIA>().FirstOrDefault(c => (c.TENQUOCGIA.ToUpper() == quocgia.TENQUOCGIA.ToUpper() ||
                c.VIETTAT.ToUpper() == quocgia.VIETTAT.ToUpper() ||
                c.MABUUCHINH.ToUpper() == quocgia.MABUUCHINH.ToUpper()) && c.MAQUOCGIA != quocgia.MAQUOCGIA) == null;
            return result;
        }

        public static bool Check_Delete_QUOCGIA(string[] strings)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<TINHTHANH>().FirstOrDefault(c => strings.Contains(c.MAQUOCGIA + "")) != null;
            return result;
        }

        public static IEnumerable<SelectListItem> Get_List()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Quốc Gia} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<QUOCGIA>().OrderBy(c => c.TENQUOCGIA), "MAQUOCGIA", "TENQUOCGIA"));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Data()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Quốc Gia} ---", Value = "" });
            list.AddRange(dataContext.GetTable<QUOCGIA>().Where(c => c.ISDATA.HasValue ? c.ISDATA.Value : false).OrderBy(c => c.TENQUOCGIA)
                .Select(c => new SelectListItem() { Text = c.TENQUOCGIA, Value = c.MAQUOCGIA + "", Selected = c.ISDEFAULT ?? false }));
            return list;
        }

    }
}
